import { RouteLocationNormalized, Router } from 'vue-router'
import storage, { IData } from '@/utils/storage'
class Guard {
	constructor(private router: Router) {}

	public run() {
		this.router.beforeEach((to, from) => {
			const token = storage.get('token')
			if (this.requireLogin(to, token)) {
				return { name: 'login' }
			}
		})
	}
	private requireLogin(route: RouteLocationNormalized, token: IData | null) {
		return Boolean(route.meta.auth && !token)
	}
}

export default (router: Router) => {
	new Guard(router).run()
}
